package cn.meizhu.org.service.impl;

import cn.meizhu.base.service.impl.BaseServiceImpl;
import cn.meizhu.org.domain.Department;
import cn.meizhu.org.mapper.DepartmentMapper;
import cn.meizhu.org.query.DepartmentQuery;
import cn.meizhu.org.service.IDepartmentService;
import cn.meizhu.util.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        System.out.println(department);
        departmentMapper.save(department);
        update(department);
    }


    @Override
    @Transactional
    public void update(Department department) {
        if(department.getParent()==null){
            department.setDirPath("/"+department.getId());
        }else {
            Department parent = departmentMapper.findById(department.getParent().getId());
            department.setDirPath(parent.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }


    @Override
    public void patchDelDepartmentByIds(Long[] ids) {
        departmentMapper.patchDelDepartmentByIds(ids);
    }

    @Override
    public List<Department> loadDeptTree() {
        return departmentMapper.loadDeptTree();
    }
}
